
//
//  CourseSearchNavView.m
//  ShowYork
//
//  Created by hxh on 2020/8/30.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonSearchNavView.h"

@interface LessonSearchNavView ()<UITextFieldDelegate>
@property (nonatomic,strong)UIView *searchView;
@property (nonatomic,strong)UIButton *centerBtn;
@property (nonatomic,strong)UIImageView *searchImgView;
@property (nonatomic,strong)UIImageView *checkImgView;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UITextField *searchTF;
@end

@implementation LessonSearchNavView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{


    self.searchView = [[UIView alloc]init];
    self.searchView.layer.cornerRadius = 15;
    self.searchView.layer.borderColor = [RPAppManager themColor].CGColor;
    self.searchView.layer.borderWidth = 1;
    self.searchView.layer.masksToBounds = YES;
    [self addSubview:self.searchView];
    
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn addTarget:self action:@selector(cancelBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(51, 51, 51, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(14);
    [self addSubview:self.cancelBtn];
    
    self.searchImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_search"]];
    [self addSubview:self.searchImgView];
    
    self.searchTF = [[UITextField alloc]init];
    self.searchTF.textColor = RGBA(51, 51, 51, 1);
    self.searchTF.font = font(14);
    self.searchTF.delegate = self;
    self.searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF.enablesReturnKeyAutomatically = YES;
    [self.searchTF addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:self.searchTF];

    
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self);
        make.right.equalTo(self.cancelBtn.mas_left);
        make.left.equalTo(self).offset(16);
    }];

    [self.searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.searchView).offset(8);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
      
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.right.equalTo(self);
        make.width.mas_equalTo(50);
    }];
    
    [self.searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.searchView);
        make.left.equalTo(self.searchImgView.mas_right).offset(3);
        make.right.equalTo(self.searchView.mas_right).offset(-5);
    }];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldBeginEditing)]) {
        [self.delegate textFieldBeginEditing];
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textSearch:)]) {
        [self.delegate textSearch:textField.text];
    }
    return YES;
}
- (void)textDidChange:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textChange:)]) {
        [self.delegate textChange:textField.text];
    }
}
- (void)cancelBtnMethod{
    
    [self.searchTF resignFirstResponder];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancel)]) {
        [self.delegate cancel];
    }
}
- (void)textEditing{
    [self.searchTF resignFirstResponder];
}
- (void)clearText{
    self.searchTF.text = @"";
}
- (void)setSearchTitle:(NSString *)searchTitle{
    self.searchTF.text = searchTitle;
}
- (void)setSearchPlaceTitle:(NSString *)searchPlachTitle{
    self.searchTF.placeholder = searchPlachTitle;
    [self.searchTF becomeFirstResponder];
}

@end
